package com.fenghongzhang.day007;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

    private Button putValues;
    private Button getValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        putValues = (Button) findViewById(R.id.putValues);
        getValues = (Button) findViewById(R.id.getValues);


        getValues.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences abc = getSharedPreferences("abc", MODE_PRIVATE);
                String name = abc.getString("name","李白");
                Toast.makeText(Main2Activity.this, name, Toast.LENGTH_SHORT).show();

                String string = abc.getString("ad发送到发送到发送到", "李白");
                Toast.makeText(Main2Activity.this, string, Toast.LENGTH_SHORT).show();

                int age = abc.getInt("age", 100);
                int ageaaa = abc.getInt("ageaaa", 100);
            }
        });


        putValues.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取到sp对象
                //xml的文件名称 -如果有就打开,如果没有就创建
                SharedPreferences sp = getSharedPreferences("ab", MODE_PRIVATE);
                //编辑器
                SharedPreferences.Editor edit = sp.edit();

                edit.putString("name","猴子");
                edit.putInt("age",10);
                edit.putBoolean("isMan",true);
                edit.putFloat("number",1.11f);
                edit.putLong("bigNum",1000l);

                //提交
                edit.commit();
            }
        });

    }
}
